//
//  FuJinErViewController.m
//  GOGOTravel
//
//  Created by dllo on 16/3/5.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "FuJinErViewController.h"
#import "fUjinXiangJingTableViewCell.h"
#import "FuJinXiangqingERTableViewCell.h"
#import "FUjinxiangQing.h"
#import "FuJin.h"
#import "FuJinMoreViewController.h"
#import "FuJinXqViewController.h"
#import "ExperienceDetailsViewController.h"
#import "GiFHUD.h"
@interface FuJinErViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSDictionary *dicAll;
@property (nonatomic, retain) NSMutableArray *imageArr;
@property (nonatomic, retain) BaseTableView *tableView;
@property (nonatomic, retain) UIImageView *imageView1;
@property (nonatomic, retain) NSMutableDictionary *dicData;/**<  装基本概况的字典 */
@property (nonatomic, retain) NSMutableArray *allValues;/**<  装所有的values */

@property (nonatomic, retain) UIWebView *phoneCallWeb;

@end

@implementation FuJinErViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)dealloc{
    [_dicAll release];
    [_imageArr release];
    [_tableView release];
    [_imageView1 release];
    [_allValues release];
    [_dicData release];
    [_phoneCallWeb release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.9 green:0.29 blue:0.44 alpha:1];
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.imageArr = [NSMutableArray array];
   self.dicData = [NSMutableDictionary dictionary];
    
    self.allValues = [NSMutableArray array];
    
    [self handleData];
    
    [self createTableView];
    
   
    [GiFHUD setGifWithImageName:@"b879f2e742d94b2a8f2362d0ca7e18c7.gif"];
    [GiFHUD show];
    
    
}

- (void)createSubView{
    
    self.imageView1 = [[UIImageView alloc] init];
    _imageView1.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 80)/2, 20 , 80, 80);
    
    _imageView1.backgroundColor = [UIColor redColor];
    _imageView1.layer.cornerRadius = 40;
    _imageView1.layer.masksToBounds = YES;
    _imageView1.image = [UIImage imageNamed:@"88c6385e6a21f1975f1a68a7ca1a9a35.jpg"];
    _imageView1.layer.borderColor = [UIColor whiteColor].CGColor;
    _imageView1.layer.borderWidth = 2;
    
    [_tableView addSubview:_imageView1];
    
    [_imageView1 release];
    
    
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 300)/2, 100, 300, 50);
    
    label.text = _dicAll[@"name"];
    label.font = [UIFont systemFontOfSize:19];
    label.textAlignment = NSTextAlignmentCenter;
    label.numberOfLines = 2;
    [_tableView addSubview:label];
    [label release];
    
    UILabel *label1 = [[UILabel alloc] init];
    label1.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 270, 140, 100, 20);
    
    NSString *str = [NSString stringWithFormat:@"%@ 人去过", _dicAll[@"visited_count"]];
    
    label1.text = str;
    label1.font = [UIFont systemFontOfSize:12];
    label1.textColor = [UIColor blueColor];
    [_tableView addSubview:label1];
    [label1 release];
    
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.frame = CGRectMake(label1.frame.size.width + [UIScreen mainScreen].bounds.size.width - 270 , 140, 100, 20);
    label2.textColor = [UIColor blueColor];
    NSString *str2 = [NSString stringWithFormat:@"%@人 希望去旅行", _dicAll[@"popularity"]];
    
    label2.text = str2;
    label2.font = [UIFont systemFontOfSize:12];
    
    [_tableView addSubview:label2];
    [label2 release];

    
    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(20, 185, [UIScreen mainScreen].bounds.size.width / 3 - 30, 2);
    view.backgroundColor = [UIColor lightGrayColor];
    
    [_tableView addSubview:view];
    
    [view release];
    
    UIView *view1 = [[UIView alloc] init];
    view1.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 3 * 2 + 10, 185, [UIScreen mainScreen].bounds.size.width / 3-30, 2);
    view1.backgroundColor = [UIColor lightGrayColor];
    
    [_tableView addSubview:view1];
    
    [view1 release];
    
    UILabel *label3 = [[UILabel alloc] init];
    label3.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 3-5 ,165, [UIScreen mainScreen].bounds.size.width / 3+10, 40);
    label3.text = @"《*面包印象*》";
    label3.textAlignment = NSTextAlignmentCenter;
    label3.font = [UIFont systemFontOfSize:16];
    [_tableView addSubview:label3];
    [label3 release];
    
    
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button4.frame = CGRectMake((self.view.frame.size.width - 200 )/2, 500, 200, 40);
    
    [button4 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button4 setTitle:@"更多印象" forState:UIControlStateNormal];
    button4.backgroundColor = [UIColor whiteColor];
    button4.layer.cornerRadius = 10;
    button4.layer.masksToBounds = YES;
    button4.layer.borderColor = [UIColor grayColor].CGColor;
    button4.layer.borderWidth = 1;
    [button4 addTarget:self action:@selector(button4Action:) forControlEvents:UIControlEventTouchUpInside];
    
    [_tableView addSubview:button4];
    
    
    
    UIView *view3 = [[UIView alloc] init];
    view3.frame = CGRectMake(20, 600, [UIScreen mainScreen].bounds.size.width / 3 - 30, 2);
    view3.backgroundColor = [UIColor lightGrayColor];
    
    [_tableView addSubview:view3];
    
    [view3 release];
    
    UIView *view4 = [[UIView alloc] init];
    view4.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 3 * 2 + 10, 600, [UIScreen mainScreen].bounds.size.width / 3-30, 2);
    view4.backgroundColor = [UIColor lightGrayColor];
    
    [_tableView addSubview:view4];
    
    [view4 release];
    
    UILabel *label5 = [[UILabel alloc] init];
    label5.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 3-10 , 580, [UIScreen mainScreen].bounds.size.width / 3+15, 40);
    label5.text = @"《*基本信息*》";
    label5.textAlignment = NSTextAlignmentCenter;
    label5.font = [UIFont systemFontOfSize:17];
    [_tableView addSubview:label5];
    [label5 release];
    
    
}

- (void)button4Action:(UIButton *)button{
    FuJinMoreViewController *FIJINmORE = [[FuJinMoreViewController alloc] init];
    FIJINmORE.model = _model;
    [self.navigationController pushViewController:FIJINmORE animated:YES];
    [FIJINmORE release];
}


- (void)createTableView{
    
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor whiteColor];
       _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    [_tableView release];
   
    
    [_tableView registerClass:[BaseTableViewCell class] forCellReuseIdentifier:@"BaseTableViewCell"];
    
    [_tableView registerClass:[fUjinXiangJingTableViewCell class] forCellReuseIdentifier:@"fUjinXiangJingTableViewCell"];
    
    [_tableView registerClass:[FuJinXiangqingERTableViewCell class] forCellReuseIdentifier:@"FuJinXiangqingERTableViewCell"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
   
    
    return 3 + _allValues.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BaseTableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }else if(indexPath.row == 1 ) {
        
        fUjinXiangJingTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:@"fUjinXiangJingTableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.arr = _imageArr;
        
        return cell;
    }else if (indexPath.row == 2){
        BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BaseTableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        
        FuJinXiangqingERTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FuJinXiangqingERTableViewCell"];
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
        FUjinxiangQing *model = _allValues[indexPath.row - 3];
        cell.model = model;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 3) {
        
        FUjinxiangQing *model = _allValues[0];
        
        FuJinXqViewController *FUXQ = [[FuJinXqViewController alloc] init];
        
        FUXQ.model = model;
        [self.navigationController pushViewController:FUXQ animated:YES];
        [FUXQ release];
        
    }
    
    if (indexPath.row == 4) {
        FUjinxiangQing *model = _allValues[1];
        
        FuJinXqViewController *FUXQ = [[FuJinXqViewController alloc] init];
        
        FUXQ.model = model;
        [self.navigationController pushViewController:FUXQ animated:YES];
        [FUXQ release];
    }
    
    //电话号码一行
    if (indexPath.row == 5) {
        FUjinxiangQing *model = _allValues[2];
        if ([model.text1 isEqual:[NSNull null]] || [model.text1 isEqualToString:@""]) {
            
        }else{
            
            NSString *phoneNum = model.text1;
            NSURL *phoneUrl = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phoneNum]];
            if (!_phoneCallWeb) {
                self.phoneCallWeb = [[UIWebView alloc] initWithFrame:CGRectZero];
            }
            
            [_phoneCallWeb loadRequest:[NSURLRequest requestWithURL:phoneUrl]];
            
            
        }
        
    }
    
    
    if (indexPath.row == 6) {
        FUjinxiangQing *model = _allValues[3];
        if ([model.text1 isEqual:[NSNull null]] || [model.text1 isEqualToString:@""]) {
            
        }else{
            ExperienceDetailsViewController *ec = [[ExperienceDetailsViewController alloc] init];
            ec.url = model.text1;
            [self.navigationController pushViewController:ec animated:YES];
            [ec release];
        }
        
        
    }
    
    if (indexPath.row == 7) {
        
         FUjinxiangQing *model = _allValues[4];
        if ([model.text1 isEqual:[NSNull null]] || [model.text1 isEqualToString:@""]) {
            
        }else{
            
            FuJinXqViewController *FUXQ = [[FuJinXqViewController alloc] init];
            
            FUXQ.model = model;
            [self.navigationController pushViewController:FUXQ animated:YES];
            [FUXQ release];
        }

       
        
    }
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row  <= 2) {
        return 220;
    }else{
        
        return 160;
    }
    
}



- (void)handleData{
    
    NSNumber *type = _model.type;
    NSNumber *idStr = _model.id;
    
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/", type, idStr];
    
    [AFNetWorkingHandle GETRequestWithURLString:str completionWithSuccess:^(id result) {
        
        
        self.dicAll = result;
        
        NSArray *arr = _dicAll[@"hottest_places"];
        
        
        
        for (NSDictionary *dic in arr) {
            NSString *str = dic[@"photo_s"];
            if (str != nil) {
                [_imageArr addObject:str];
            }

        }
        
        for (NSString *key in _dicAll.allKeys) {
           
            if ([key isEqualToString:@"description"]) {
                FUjinxiangQing *model = [[FUjinxiangQing alloc] init];
                [model setValue:_dicAll[key] forKey:@"text1"];
                [model setValue:key forKey:@"title1"];
                [_allValues addObject:model];
            }
            
            if ([key isEqualToString:@"address"]) {
                FUjinxiangQing *model = [[FUjinxiangQing alloc] init];
                [model setValue:_dicAll[key] forKey:@"text1"];
                [model setValue:key forKey:@"title1"];
                [_allValues addObject:model];
            }
            if ([key isEqualToString:@"arrival_type"]) {
                FUjinxiangQing *model = [[FUjinxiangQing alloc] init];
                [model setValue:_dicAll[key] forKey:@"text1"];
                [model setValue:key forKey:@"title1"];
                [_allValues addObject:model];
            }
            
            if ([key isEqualToString:@"opening_time1"]) {
                FUjinxiangQing *model = [[FUjinxiangQing alloc] init];
                [model setValue:_dicAll[key] forKey:@"text1"];
                [model setValue:key forKey:@"title1"];
                [_allValues addObject:model];
            }
            
            if ([key isEqualToString:@"tel"]) {
                FUjinxiangQing *model = [[FUjinxiangQing alloc] init];
                [model setValue:_dicAll[key] forKey:@"text1"];
                [model setValue:key forKey:@"title1"];
                [_allValues addObject:model];
            }
            
            if ([key isEqualToString:@"website"]) {
                FUjinxiangQing *model = [[FUjinxiangQing alloc] init];
                [model setValue:_dicAll[key] forKey:@"text1"];
                [model setValue:key forKey:@"title1"];
                [_allValues addObject:model];
            }
            
            
        }
        
         self.navigationItem.title = _dicAll[@"name"];
        
        
        [self createSubView];
        
        [_tableView reloadData];
        
        
        [GiFHUD dismiss];
    } withFailure:^(id error) {
        
    }];
    
    
    
    
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
